// pages/order/detail/detail.js
import {reqGetOrderAddress,reqGetCartgoodsTrad,reqGetGoodsInfoBuyNow,reqSubmitOrder,reqGetPaymentParams,reqGetPayStatus} from '../../../utils/api'
import moment from 'moment'
import { showToast } from '../../../utils/tips'
Page({

  //获取默认的收货人地址
  async getOrderAddressList(){
    const result = await reqGetOrderAddress()
    // console.log(result)
    if(result.code === 200){
      this.setData({
        userAddress:result.data,
        userAddressId:result.data.id
      })
    }
  },

  //去结算跳到订单详情列表
  async getOrderListByTrade(){
    const result = await reqGetCartgoodsTrad()
    // console.log(result)
    if(result.code === 200){
      this.setData({
        cartList:result.data.cartVoList,
        totalPrice:result.data.totalAmount,
        totalCount:result.data.totalCount
      })
    }
  },

  //立即购买跳到订单详情列表
  async getOrderListByNow(){
    const {goodsId,blessing} = this.data
    const result = await reqGetGoodsInfoBuyNow({goodsId,blessing})
    // console.log(result)
    if(result.code === 200){
      this.setData({
        cartList:result.data.cartVoList,
        totalPrice:result.data.totalAmount,
        totalCount:result.data.totalCount
      })
    }
  },

  //弹出日期卡
  handleShowPopup(){
    this.setData({
      isShowPopup:true
    })
  },

  //关闭日期卡
  onClose(){
    this.setData({
      isShowPopup:false
    })
  },

  //日期卡确认时间
  handleDatePickerConfrim(e){
    // console.log(e)
    this.setData({
      deliveryDate:moment(e.detail).format('YYYY-MM-DD'),
      isShowPopup:false
    })
  },

    //去结算
   async handleGoBuy(){
      const {
        buyName,
        buyPhone,
        deliveryDate,
        remarks,
        cartList,
        userAddressId
      } = this.data
      if (!buyName.trim()) return showToast('订购人姓名不能为空', 'none')
      if (!buyPhone.trim()) return showToast('订购人电话不能为空', 'none')
      if (!deliveryDate.trim()) return showToast('期望送达不能为空', 'none')
      
      //发送请求
      const result = await reqSubmitOrder({ buyName,
        buyPhone,
        deliveryDate,
        remarks,
        cartList,
        userAddressId})
        console.log(result.code)
        console.log(result)
      if(result.code === 200){
        this.setData({
          orderNo:result.data //记录订单号
        })
        this.getPaymentParams(result.data)
      }
    },

    //获取支付参数并支付
    async getPaymentParams(orderNo){
     const result = await reqGetPaymentParams(orderNo)
    //  console.log('hh',result)
    if(result.code === 200){
      wx.requestPayment({
        nonceStr: result.data.nonceStr,
        package: result.data.package,
        paySign: result.data.paySign,
        timeStamp: result.data.timeStamp,
        signType:result.data.signType,
        success:res=>{
          // 再次查询订单支付状态
          this.getPayStatus()
        },
        fail:error=>{
          showToast(result.data,'error')
        }
      })
    }
    },

    //查询订单支付状态
   async getPayStatus(){
      const reusult = await reqGetPayStatus(this.data.orderNo)
      if(result.code === 200){
        wx.showToast({
          title: '支付成功',
          success:()=>{
            wx.navigateTo({
              url: '/pages/order/result/result',
            })
          }
        })
      }
    },

  data: {
    orderNo:'',//订单编号
    userAddress:{},//订单地址信息
    cartList: [], // 待支付的订单列表
    totalPrice: 0, // 订单总金额 
    totalCount: 0, // 订单商品总数量
    buyName:'', //订购人姓名
    buyPhone:'', //订购人的手机号
    deliveryDate:'', //期望送达日期
    remarks:'', //订单备注
    isShowPopup: false, // 日期选项卡
    nowDate: new Date().getTime(),
    minDate: new Date().getTime(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {goodsId,blessing} = options
    this.setData({
      goodsId,
      blessing
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const {goodsId} = this.data
    this.getOrderAddressList()
   
    if(goodsId){
      this.getOrderListByNow()
    }else{
      this.getOrderListByTrade()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})